package com.jianjun.project.system.post;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.Method;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jianjun.common.constant.HttpStatus;
import com.jianjun.common.util.SecurityUtils;
import com.jianjun.framework.web.domain.AjaxResult;
import com.jianjun.framework.web.interceptor.DataScope;
import com.jianjun.framework.web.page.TableDataInfo;
import com.jianjun.framework.web.render.ExcelWriterRender;
import com.jianjun.project.common.model.SysPost;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 岗位信息操作处理
 *
 * @author source
 */
@Path(value = "/system/post")
public class SysPostController extends Controller {
    @Inject
    SysPostService postService;

    @DataScope(permission = "system:post:list")
    public void list() {
        SysPost post = JsonKit.parse(getKv().toJson(), SysPost.class);
        Page<SysPost> page = postService.paginate(post);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(page.getList());
        rspData.setTotal(page.getTotalRow());
        renderJson(rspData);
    }

    @DataScope(permission = "system:post:export")
    public void export() {
        SysPost post = JsonKit.parse(getKv().toJson(), SysPost.class);
        List<SysPost> list = postService.selectPostList(post);
        List<Map<String, Object>> mapList = list.stream().map(Model::toMap).collect(Collectors.toList());
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.write(mapList, true);
        render(new ExcelWriterRender(writer, post.getClass().getSimpleName()));
    }

    @DataScope(permission = "system:post:query,system:post:add,system:post:edit,system:post:remove")
    public void index() {
        String method = getRequest().getMethod();
        //查询
        if (Method.GET.name().equals(method)) {
            Long postId = getParaToLong();
            renderJson(AjaxResult.success(postService.selectPostById(postId)));
        }
        //新增
        if (Method.POST.name().equals(method)) {
            SysPost post = JsonKit.parse(getRawData(), SysPost.class);
            if (!postService.checkPostNameUnique(post)) {
                renderJson(AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在"));
                return;
            } else if (!postService.checkPostCodeUnique(post)) {
                renderJson(AjaxResult.error("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在"));
                return;
            }
            post.setCreateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(postService.insertPost(post)));
        }
        //修改
        if (Method.PUT.name().equals(method)) {
            SysPost post = JsonKit.parse(getRawData(), SysPost.class);
            if (!postService.checkPostNameUnique(post)) {
                renderJson(AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在"));
                return;
            } else if (!postService.checkPostCodeUnique(post)) {
                renderJson(AjaxResult.error("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在"));
                return;
            }
            post.setUpdateBy(SecurityUtils.getUsername());
            renderJson(AjaxResult.success(postService.updatePost(post)));
        }
        //删除
        if (Method.DELETE.name().equals(method)) {
            renderJson(AjaxResult.ajax(postService.deletePostByIds(Convert.toLongArray(getPara()))));
        }
    }

    /**
     * 获取岗位选择框列表
     */
    public void optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        renderJson(AjaxResult.success(posts));
    }
}
